<template>
    <div class="login-wrap">
        <div class="bgView">
            <!-- <vue-particles color="#dedede" :particleOpacity="0.6" :particlesNumber="80" shapeType="star" :particleSize="0.1" linesColor="#FFFFFF" :linesWidth="1" :lineLinked="true" :lineOpacity="0.4" :linesDistance="150" :moveSpeed="1" :hoverEffect="true" hoverMode="grab" :clickEffect="false" clickMode="repulse" class="cash" style="height: 100%;width: 100%;">
            </vue-particles> -->
        </div>
        <div class="ms-login">
            <div class="ms-title">
                <img src="../../assets/ryzLogo/logo-1.png" class="logo" />
                <!-- <div class="title">后台管理系统</div> -->
            </div>
            <el-form :model="param" :rules="rules" ref="login" label-width="0px" class="ms-content">
                <el-form-item prop="mobile" style="margin-bottom: 20px;">
                    <el-input v-model="param.mobile" placeholder="请输入登录账号">
                        <el-button slot="prepend" icon="el-icon-user"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item prop="password" style="margin-bottom: 30px;">
                    <el-input type="password" placeholder="请输入登录密码" v-model="param.password" @keyup.enter.native="submitForm()">
                        <el-button slot="prepend" icon="el-icon-lock"></el-button>
                    </el-input>
                </el-form-item>
                <div class="login-btn">
                    <el-button type="warning" @click="submitForm()">登录</el-button>
                </div>
            </el-form>
        </div>
    </div>
</template>

<script>
    export default {
        data: function() {
            return {
                param: {
                    mobile: '',
                    password: '',
                },
                rules: {
                    mobile: [{ required: true, message: '请输入用户名', trigger: 'blur' },
                        // { min: 11, max: 11, message: '1开头的11位手机号码', trigger: 'blur' }
                    ],
                    password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
                },
            };
        },
        created() {
            this.$store.commit('SET_CUSTOME_SHOW', false)
        },
        methods: {
            submitForm() {
                this.$refs.login.validate(async valid => {
                    if (valid) {
                        let params = {
                            username: this.param.mobile,
                            password: this.$md5(this.param.password)
                        }
                        let res = await this.$axios.post('api/root/login/index', params)
                        // .then(res => {
                        console.log('登录数据', res);
                        if (res.data.code == 1) {
                            let token = res.data.data.user.token.access_token
                            let user_id = res.data.data.user.id
                            // console.log(user_id)
                            localStorage.setItem('UserName', res.data.data.name)
                            this.$store.commit("SET_USERID", user_id);
                            this.$store.commit("SET_TOKEN", token);
                            console.log('2');
                            this.getUserRoot()
                        } else {
                            this.$message.error(res.data.msg);
                        }

                        //   })
                        //   .catch(err => {
                        // this.$message.error(err);
                        //   })
                    } else {
                        this.$message.error('请输入账号和密码');
                        return false;
                    }
                });
            },

            // 获取当前权限列表
            getUserRoot() {
                this.$http.post('/api/root/Rbac/getMineRole')
                    .then(res => {
                        console.log(res, '权限列表');
                        if (res.code == 1) {
                            let root_router = JSON.stringify(res.data)
                            this.$store.commit("SET_ROOT_ROUTER", root_router);
                            this.$router.push('/manaindex')
                            this.$message.success('登录成功');
                            console.log('1');
                        }
                    })
            }
        },
    };
</script>

<style scoped lang='less'>
    .login-wrap {
        position: relative;
        width: 100%;
        height: 100%;
        background-image: url(../../assets/bg.jpg);
        background-repeat: no-repeat;
        background-size: 100% 100%;
    }

    .bgView {
        position: absolute;
        width: 100%;
        height: 100%;
        background: rgba(0, 0, 0, .6);
    }

    .ms-title {
        width: 100%;
        // line-height: 50px;
        /* text-align: center; */
        font-size: 20px;
        color: #fff;
        border-bottom: 1px solid #ddd;
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 20px;

        .logo {
            width: 240px;
            height: 80px;
        }

        .title {
            color: #fff;
            font-weight: 600;
            font-size: 16px;
        }
    }

    .ms-login {
        position: absolute;
        left: 50%;
        top: 50%;
        width: 450px;
        /* margin: -190px 0 0 -175px; */
        transform: translate(-50%, -50%);
        border-radius: 5px;
        background: rgba(255, 255, 255, 0.1);
        overflow: hidden;
    }

    .ms-content {
        padding: 30px 45px;
    }

    .login-btn {
        text-align: center;
    }

    .login-btn button {
        width: 100%;
        font-size: 16px;
        /* line-height: 36px; */
        padding: 10px 0;
        margin-bottom: 10px;
    }
</style>